package com.shengqi.xinnongren.work.ui.user.activities;

import android.support.annotation.NonNull;
import android.support.v7.widget.GridLayoutManager;
import android.support.v7.widget.RecyclerView;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.view.ViewGroup;
import android.widget.EditText;
import android.widget.TextView;

import com.shengqi.xinnongren.R;
import com.shengqi.xinnongren.common.base.BaseMvpActivity;
import com.shengqi.xinnongren.common.utils.ToastHelper;
import com.shengqi.xinnongren.work.ui.user.presenter.ReChargePresenter;
import com.shengqi.xinnongren.work.ui.user.view.ReChargeView;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.ButterKnife;
import butterknife.OnClick;

/**
 * 充值
 */
public class ReChargeActivity extends BaseMvpActivity<ReChargeView, ReChargePresenter> implements ReChargeView {

    @BindView(R.id.recyclerView)
    RecyclerView mRecyclerView;
    @BindView(R.id.edt_other_money)
    EditText mEdtOtherMoney;

    private static int LAST_POSITION = 8;

    private ReChargeAdapter mAdapter;
    private List<String> mList;
    private String mMoney;


    @Override
    protected int setContentLayoutRes() {
        return R.layout.activity_re_charge;
    }

    @Override
    protected void initView() {
        mList = new ArrayList<>();
        mRecyclerView.setLayoutManager(new GridLayoutManager(this, 3));
        mAdapter = new ReChargeAdapter();
        mRecyclerView.setAdapter(mAdapter);
    }

    @Override
    protected void initData() {
        mPresenter.getData();
    }

    /**
     * 确定充值
     */
    @OnClick(R.id.tv_confirm_recharge)
    void confirmRecharge() {
        if (mAdapter.getChoosePosition() == LAST_POSITION) {
            mMoney = mEdtOtherMoney.getText().toString();
            if (TextUtils.isEmpty(mMoney)) {
                ToastHelper.getInstance(this).showWarn("请输入金额");
                return;
            }
        } else
            mMoney = mList.get(mAdapter.getChoosePosition());

        ReChargePayActivity.start(this, mMoney, 1);
    }

    @Override
    protected ReChargePresenter createPresenter() {
        return new ReChargePresenter();
    }

    @Override
    public void showLoadingView() {
        showLoadingDialog();
    }

    @Override
    public void hideLoadingView() {
        hideLoadingDialog();
    }

    @Override
    public void onNetworkError() {
        hideLoadingView();
    }

    @Override
    public void showReChargeData(List<String> data) {
        mList.clear();
        mList.addAll(data);
        mList.add("其它金额");
        LAST_POSITION = data.size();
        mAdapter.notifyDataSetChanged();
    }


    class ReChargeAdapter extends RecyclerView.Adapter<ReChargeAdapter.ReChargeHolder> {

        private int mChoosePosition = 0;

        public int getChoosePosition() {
            return mChoosePosition;
        }

        @NonNull
        @Override
        public ReChargeHolder onCreateViewHolder(@NonNull ViewGroup parent, int viewType) {
            return new ReChargeHolder(LayoutInflater.from(ReChargeActivity.this).inflate(R.layout.item_recharge, null));
        }

        @Override
        public void onBindViewHolder(@NonNull ReChargeHolder holder, int position) {

            if (position == LAST_POSITION) {
                holder.item_content.setText(mList.get(position));
            } else
                holder.item_content.setText(mList.get(position) + "元");

            if (position == mChoosePosition) {
                holder.item_content.setBackgroundResource(R.mipmap.recharge_selected);
            } else
                holder.item_content.setBackgroundResource(R.drawable.shape_bg_et_rect_white);

            holder.itemView.setOnClickListener(v -> {
                mChoosePosition = position;
                notifyDataSetChanged();

                if (position == LAST_POSITION) {
                    mEdtOtherMoney.setEnabled(true);
                } else {
                    mEdtOtherMoney.setEnabled(false);
                    mEdtOtherMoney.setText("");
                }
            });
        }

        @Override
        public int getItemCount() {
            return mList.size();
        }

        class ReChargeHolder extends RecyclerView.ViewHolder {

            @BindView(R.id.item_content)
            TextView item_content;

            public ReChargeHolder(View itemView) {
                super(itemView);
                ButterKnife.bind(this, itemView);
            }
        }
    }
}
